package com.example.secondhand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.secondhand.common.enums.StatusEnum;
import com.example.secondhand.service.UserService;
import com.example.secondhand.dao.UserDao;
import com.example.secondhand.model.shUser;
import com.example.secondhand.vo.PageVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;



@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    /**
     * 查询一个用户的公开信息
     * @param id
     * @return
     */
    public shUser getUser(Long id){
        return userDao.selectById(id);
    }

    /**
     * 登录，安全问题未解决
     * @param accountNumber
     * @param userPassword
     * @return
     */
    public shUser userLogin(String accountNumber, String userPassword){
        return userDao.userLogin(accountNumber,userPassword);
    }

    /**
     *注册
     * @param userModel
     * @return
     */
    public boolean userSignIn(shUser userModel){
        return userDao.insert(userModel) == 1;
    }

    /**
     *修改用户公开信息，未验证用户身份
     * @param userModel
     * @return
     */
    public boolean updateUserInfo(shUser userModel){
        if(userModel.getUserAllow()== StatusEnum.DELETE.getCode()){
            return userDao.deleteById(userModel)==1;
        }
        return userDao.updateById(userModel)==1;
    }
    public boolean updateUserStatus(shUser userModel){
        userModel.setUserAllow(StatusEnum.ENABL.getCode());
        if(userModel.getUserStatus()==0){
            userModel.setUserStatus(StatusEnum.DISABLE.getCode());
        }
        if(userModel.getUserStatus()==1){
            userModel.setUserStatus(StatusEnum.ENABL.getCode());
        }
        return userDao.updateById(userModel)==1;
    }

    /**
     * 修改密码
     * @param newPassword
     * @param oldPassword
     * @param id
     * @return
     */
    public boolean updatePassword(String newPassword, String oldPassword,Long id){
        return userDao.updatePassword(newPassword,oldPassword,id)==1;
    }

    public PageVo<shUser> getUserByStatus(int status, int page , int nums){
        List<shUser> list;
        Long count= Long.valueOf(0L);
        if(status==0){
            count= Long.valueOf(userDao.countNormalUser());
            list=userDao.getNormalUser((page-1)*nums, nums);
        }else {
            count= Long.valueOf(userDao.countBanUser());
            list=userDao.getBanUser((page-1)*nums, nums);
        }
        return new PageVo<>(list,count);
    }

    @Override
    public PageVo<shUser> getUserByAllow(int page, int nums) {
        Page page1 = new Page(page,nums);
        LambdaQueryWrapper<shUser> wrapper = new LambdaQueryWrapper();
        wrapper.eq(shUser::getUserAllow,"0");
        userDao.selectPage(page1,wrapper);

        return new PageVo<>(page1.getRecords(),page1.getTotal());
    }

    @Override
    public boolean updateMoney(String id, String money) {
        shUser shUser = userDao.selectById(id);
        shUser.setMoney(Double.valueOf(money));
        if(userDao.updateById(shUser)==1){
            return true;
        }else {
            return false;
        }
    }
}
